package com.lill.system.serivce.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.util.IdUtil;
import com.lill.system.dao.ISysUserLineDao;
import com.lill.system.dao.ISysUserLineHistoryDao;
import com.lill.system.model.SysUserLineEntity;
import com.lill.system.model.SysUserLineHistoryEntity;
import com.lill.system.service.ISysUserLineService;

@Service("com.lill.system.serivce.impl.SysUserLineServiceImpl")
public class SysUserLineServiceImpl implements ISysUserLineService {

	@Autowired
	private ISysUserLineDao sysUserLineDao;
	@Autowired
	private ISysUserLineHistoryDao sysUserLineHistoryDao;
	
	@Override
	public List<String> querySysUserOnlineList(String userId) {
		List<String> codeList=new ArrayList<String>();
		Map<String, Object> param=new HashMap<String, Object>();
		param.put("id", userId);
		param.put("state", 1);
		List<SysUserLineEntity> lineList=sysUserLineDao.queryList(param);
		if(null!=lineList&&!lineList.isEmpty()){
			Set<String> codeSet=new HashSet<String>();
			for (SysUserLineEntity sysUserLineEntity : lineList) {
				if(null!=sysUserLineEntity&&null!=sysUserLineEntity.getPhoneCode()
						&&!"".equals(sysUserLineEntity.getPhoneCode().trim())){
					codeSet.add(sysUserLineEntity.getPhoneCode());
				}
			}
			if(!codeSet.isEmpty()){
				codeList.addAll(codeSet);
			}
		}
		return codeList;
	}
	
	@Override
	public int sysUserOnline(String userId, String phoneCode) {
		return userLineOpertion(userId, phoneCode, 1);
	}

	@Override
	public int sysUserOffline(String userId, String phoneCode) {
		return userLineOpertion(userId, phoneCode, 0);
	}
	
	@Override
	public boolean isUserOffline(String userId) {
		boolean result=false;
		Map<String, Object> param=new HashMap<String, Object>();
		param.put("id", userId);
		List<SysUserLineEntity> lineList=sysUserLineDao.queryList(param);
		if(null!=lineList&&!lineList.isEmpty()){
			for (SysUserLineEntity sysUserLineEntity : lineList) {
				if(sysUserLineEntity.getState()>0){
					result=true;break;
				}
			}
		}
		return result;
	}
	
	protected int userLineOpertion(String userId, String phoneCode,int userState) {
		int result=0;
		Map<String, Object> mapParam=new HashMap<String, Object>();
		mapParam.put("id", userId);
		mapParam.put("phoneCode", phoneCode);
		SysUserLineEntity sysUserLine=sysUserLineDao.queryOnlyLine(mapParam);
		if(null!=sysUserLine){
			sysUserLine.setState(userState);
			result=sysUserLineDao.update(sysUserLine);
		}else{
			sysUserLine=new SysUserLineEntity();
			sysUserLine.setId(userId);
			sysUserLine.setPhoneCode(phoneCode);
			sysUserLine.setState(userState);
			result=sysUserLineDao.save(sysUserLine);
		}
		if(StringUtils.isNotEmpty(phoneCode)){
			sysUserLineDao.updateSameEqu(sysUserLine);
		}
		
		SysUserLineHistoryEntity userLineHistory=new SysUserLineHistoryEntity();
		userLineHistory.setId(IdUtil.createIdbyUUID());
		userLineHistory.setUserId(userId);
		userLineHistory.setPhoneCode(phoneCode);
		userLineHistory.setState(userState);
		userLineHistory.setOperTime(new Date());
		sysUserLineHistoryDao.save(userLineHistory);
		return result;
	}

	@Override
	public List<Map<String, Object>> queryKPILoginDays(Map<String, Object> params) {
		return sysUserLineHistoryDao.queryKPILoginDays(params);
	}

}
